package com.atguigu.spzx.manager.service.impl;

import com.atguigu.spzx.manager.mapper.OrderInfoMapper;
import com.atguigu.spzx.manager.mapper.OrderStatisticsMapper;
import com.atguigu.spzx.manager.service.OrderStatisticsService;
import com.atguigu.spzx.model.entity.order.OrderStatistices;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class OrderStatisticsServiceImpl implements OrderStatisticsService {
    @Resource
    OrderInfoMapper orderInfoMapper;
    @Resource
    OrderStatisticsMapper orderStatisticsMapper;
    @Override
    public void generateOrderStatistics(String date) {
        //1、查询统计数据
        Map<String,Object> map =  orderInfoMapper.selectOrderStatistics(date);
        //2、保存订单统计数据
        OrderStatistices orderStatistics = new OrderStatistices();
        orderStatistics.setOrderDate(date);
        orderStatistics.setTotalAmount(new BigDecimal(map.get("totalAmount").toString()));
        orderStatistics.setTotalNum(Integer.valueOf(map.get("totalNum").toString()));
        orderStatisticsMapper.save(orderStatistics);
    }

    @Override
    public Map<String, Object> list(String begin, String end) {
        //1、根据时间区间查询统计数据集合
        List<OrderStatistices> list =  orderStatisticsMapper.selectByDate(begin,end);
        //2、按照前端的要求组装数据
        Map<String, Object> map = new HashMap<>();
        map.put("totalAmountTitle" , "订单总金额");
        map.put("totalNumTitle" , "订单总数量");
        //获取日期集合
        List<String> dateList = list.stream().map(orderStatistics -> orderStatistics.getOrderDate()).collect(Collectors.toList());
        //获取统计的总金额集合
        List<BigDecimal> totalAmountList = list.stream().map(orderStatistices -> orderStatistices.getTotalAmount()).collect(Collectors.toList());
        //获取统计的总数量集合
        List<Integer> totalNumList = list.stream().map(orderStatistices -> orderStatistices.getTotalNum()).collect(Collectors.toList());

        map.put("dateList",dateList);
        map.put("totalAmountList",totalAmountList);
        map.put("totalNumList",totalNumList);

        return map;
    }
}
